package cn.qingfeng.a004_chat_v3.server.handler;

import cn.qingfeng.a004_chat_v3.message.GmembersRequestMessage;
import cn.qingfeng.a004_chat_v3.message.GmembersResponseMessage;
import cn.qingfeng.a004_chat_v3.server.groupsession.GroupSession;
import cn.qingfeng.a004_chat_v3.server.groupsession.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Set;

@ChannelHandler.Sharable
public class GmembersHandler extends SimpleChannelInboundHandler<GmembersRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GmembersRequestMessage msg) throws Exception {
        final GroupSession groupSession = GroupSessionFactory.getSession();

        if (msg == null) {
            return;
        }

        String groupName = msg.getGroupName();
        Set<String> members = groupSession.getMembers(groupName);
        ctx.writeAndFlush(new GmembersResponseMessage(members));
    }
}